﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Modularity;
using Tanmia.Basis.ServiceInterface;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using Tanmia.Warehouse.Presentation.Scenarios.UnitScenario;
using Tanmia.Basis.ViewModel;
using Tanmia.Warehouse.Domain;
using System.Globalization;
using Tanmia.Warehouse.Presentation.Scenarios.BranchScenario;
using Tanmia.Basis.Security;
using Tanmia.Basis.Core;
using Tanmia.Warehouse.Core;
using Tanmia.Warehouse.Presentation.Scenarios.StoreScenario;

namespace Tanmia.Warehouse.Presentation
{
    public class WarehouseModule : IModule
    {
        private readonly INavigationService navigationService;
        private readonly IWindsorContainer container;

        public WarehouseModule(INavigationService navigationService, IWindsorContainer container)
        {
            this.navigationService = navigationService;
            this.container = container;
        }

        #region IModule Members

        public void Initialize()
        {
            RegisterDomains();
            RegisterViews();
        }

        private void RegisterDomains()
        {
            container.Register(Component.For<UnitDomain>().LifeStyle.Transient);
            container.Register(Component.For<BranchDomain>().LifeStyle.Transient);
            container.Register(Component.For<StoreDomain>().LifeStyle.Transient);
        }

        #endregion

        private void RegisterViews()
        {
            NavigationItem warehouseHeader = new NavigationItem { Title = "المستودعات", Description = "نظام مستودعات تنيمة", ViewName = "WarehouseModule", Parent = null };
            navigationService.RegisterNavigatorItem(warehouseHeader);

            container.RegisterView<UnitEditor, UnitViewModel, Unit>(warehouseHeader, "WindowRegion");
            container.RegisterBrowserView<UnitBrowser, UnitViewModel, Unit>(warehouseHeader, "MainRegion");

            container.RegisterView<BranchEditor, BranchViewModel, Branch>(warehouseHeader, "WindowRegion");
            container.RegisterBrowserView<BranchBrowser, BranchViewModel, Branch>(warehouseHeader, "MainRegion");

            container.RegisterView<StoreEditor, StoreViewModel, Store>(warehouseHeader, "WindowRegion");
            container.RegisterBrowserView<StoreBrowser, StoreViewModel, Store>(warehouseHeader, "MainRegion");
        }

        //private void RegisterBrowserView<TView, TViewModel, TBusinessObject>(NavigationItem parent, string regionName)
        //    where TBusinessObject : BaseBusinessObject, new()
        //    where TViewModel : BaseEditAndBrowseViewModel<TBusinessObject>
        //{
        //    if (authorizationService.IsAuthorize(string.Format("/{0}/{1}", parent.ViewName, typeof(TView).Name)))
        //    {
        //        if (!container.Kernel.HasComponent(typeof(TViewModel)))
        //            container.Register(Component.For<TViewModel>().LifeStyle.Singleton);

        //        TViewModel viewModel = container.Resolve<TViewModel>();

        //        viewModel.BrowserInfo.RegionName = regionName;
        //        viewModel.BrowserInfo.ViewName = typeof(TView).Name;

        //        container.Register(Component.For<TView>().Named(typeof(TView).Name).LifeStyle.Singleton);

        //        navigationService.RegisterNavigatorItem(new NavigationItem
        //        {
        //            Title = viewModel.BrowserInfo.Title,
        //            Description = viewModel.BrowserInfo.Description,
        //            RegionName = regionName,
        //            ViewName = typeof(TView).Name,
        //            Parent = parent
        //        });
        //    }
        //}

        //private void RegisterEditorView<TView, TViewModel, TBusinessObject>(NavigationItem parent, string regionName)
        //    where TBusinessObject : BaseBusinessObject, new()
        //    where TViewModel : BaseEditAndBrowseViewModel<TBusinessObject>
        //{
        //    if (authorizationService.IsAuthorize(string.Format("/{0}/{1}", parent.ViewName, typeof(TView).Name)))
        //    {
        //        if (!container.Kernel.HasComponent(typeof(TViewModel)))
        //            container.Register(Component.For<TViewModel>().LifeStyle.Singleton);

        //        TViewModel viewModel = container.Resolve<TViewModel>();

        //        viewModel.ViewInfo.RegionName = regionName;
        //        viewModel.ViewInfo.ViewName = typeof(TView).Name;

        //        container.Register(Component.For<TView>().Named(typeof(TView).Name).LifeStyle.Singleton);

        //        navigationService.RegisterNavigatorItem(new NavigationItem
        //        {
        //            Title = viewModel.ViewInfo.Title,
        //            Description = viewModel.ViewInfo.Description,
        //            RegionName = regionName,
        //            ViewName = typeof(TView).Name,
        //            Parent = parent
        //        });
        //    }
        //}
    }
}
